package com.ruoyi.web.controller.finance;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.finance.domain.ChargeRecord;
import com.ruoyi.finance.domain.Merchant;
import com.ruoyi.finance.mapper.MerchantMapper;
import com.ruoyi.finance.service.IChargeRecordService;
import com.ruoyi.jiekou.mapper.JieKouMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
 * 查询收费Controller
 */
@RestController
@RequestMapping("/record")
public class ChargeRecordController extends BaseController {
    @Autowired
    private IChargeRecordService chargeRecordService;

    @Autowired
    private JieKouMapper jieKouMapper;

    @Autowired
    private MerchantMapper merchantMapper;

    /**
     * 查询查询收费列表
     */
    @GetMapping("/list")
    public TableDataInfo list(ChargeRecord chargeRecord) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        List<SysRole> roles = loginUser.getUser().getRoles();
        for (SysRole sysRole : roles) {
            if (!("admin".equals(sysRole.getRoleKey()) || "yewuadmin".equals(sysRole.getRoleKey()))) {    // 非管理员
                String merchantCode = merchantMapper.selectMerchantByUserOne(new Merchant(loginUser.getUserId())).getMerchantCode();
                chargeRecord.setMerchantCode(merchantCode);
            }
        }
        startPage();
        List<ChargeRecord> list = chargeRecordService.selectChargeRecordList(chargeRecord);
        for (ChargeRecord cr : list) {
            StringBuffer sb = new StringBuffer(cr.getInterKeyword());
            sb.replace(0, 8, "***");
            int l = sb.length();
            sb.replace(l - 4, l, "***");
            cr.setInterKeyword(sb.toString());
        }
        return getDataTable(list);
    }

    /**
     * 新增查询收费
     */
    @Log(title = "查询收费", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ChargeRecord chargeRecord) {
        return toAjax(chargeRecordService.insertChargeRecord(chargeRecord));
    }

    /**
     * 修改查询收费
     */
    @Log(title = "查询收费", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ChargeRecord chargeRecord) {
        return toAjax(chargeRecordService.updateChargeRecord(chargeRecord));
    }

    @GetMapping("/getCount")
    public AjaxResult getCount(String merchantCode) {
        return success(jieKouMapper.getCount(merchantCode));
    }

}
